<?php 
class ControllerDesignTheme extends AdminController {
	
	public function index() {
		$this->document->setTitle($this->_['heading_title']);
		
		$this->data['templates'] = array();
		
		$directories = glob(DIR_APPLICATION . '../themes/*', GLOB_ONLYDIR);
		foreach ($directories as $directory) {
			$theme = basename($directory);
			if (!in_array($theme, array('javascript', 'image', 'mobile'))) {
				if (file_exists($directory . '/info.php')) {
					$this->data['templates'][$theme] = include($directory . '/info.php');
				}
				else {
					$this->data['templates'][$theme] = array('name' => $theme, 'configurable' => false);
				}
			}
		}		
		$this->display('design/theme_list.tpl');
	}
	
	public function browse () {
		$exts = array('php', 'tpl', 'js', 'css', 'html');
		
		$this->document->setTitle($this->_['heading_title']);
		
		$theme = G('theme');
		$path = G('path', '');
		
		if (empty($path)) {
			$current_path = realpath(DIR_APPLICATION . '../themes/' . $theme) . '/';
			$current_url = ROOT . 'themes/' . $theme . '/';
			$current_dir_path = '';
			$moveup_dir_path = '';
		} else {
			$current_path = realpath(DIR_APPLICATION . '../themes/' . $theme) . '/' . $path . '/';
			$current_url = ROOT . 'themes/' . $theme . '/' . $path . '/';
			$current_dir_path = $path . '/';
			$moveup_dir_path = str_replace('\\', '/', dirname($path));
			if ($moveup_dir_path == '.') $moveup_dir_path = '';
		}
		$dir_list = array();
		$file_list = array();
		if ($handle = opendir($current_path)) {
			while (false !== ($filename = readdir($handle))) {
				if ($filename{0} == '.') continue;
				if (is_dir($current_path . $filename)) {
					$dir_list[] = $filename;
				} else {
					$ext = get_file_ext($filename);
					$file_list[] = array(
						'filename' => $filename,
						'ext' => $ext,
						'editable' => in_array($ext, $exts)
					);
				}
			}
			closedir($handle);
		}
		
		$this->data['theme'] = $theme;
		$this->data['current_dir_path'] = $current_dir_path;
		$this->data['current_url'] = $current_url;
		$this->data['moveup_dir_path'] = $moveup_dir_path;
		$this->data['dir_list'] = $dir_list;
		$this->data['file_list'] = $file_list;
		
		$this->display('design/theme_browse.tpl');
	}
	
	public function fileedit() {
		$this->document->setTitle($this->_['heading_title']);
		
		$theme = G('theme');
		$path = G('path', '');
		$file = P('file', '');
		if (!$file) $file = G('file', '');
		
		$file_path = realpath(DIR_APPLICATION . '../themes/' . $theme) . '/' . ($path ? $path . '/' : '') . $file;
		
		if ($this->request->isPost() && $this->hasMP()) {
			if ($file && file_put_contents($file_path, v5_html_entity_decode(P('content')))) {
				$this->data['message'] = $this->_['text_success'];
			}
			else {
				$this->data['message'] = $this->_['error_file'];
			}
			
			$this->data['heading_title'] = $this->_['heading_title'];			
			$this->display('common/message.tpl');
			
			return;
		}
		
		$exts = array('php', 'tpl', 'js', 'css', 'html');
		
		$this->data['file'] = $file;
		$this->data['syntax'] = '';
		$this->data['content'] = '';
		if ($file && is_file($file_path)) {
			$ext = get_file_ext($file);
			if (in_array($ext, $exts)) {
				$ext == 'tpl' && $ext = 'php';
			}
			$this->data['syntax'] = $ext;
			$this->data['content'] = file_get_contents($file_path);
		}
		
		$this->display('design/theme_edit.tpl');
	}
	
	public function setting() {
		$theme = G('theme');
		
		$theme_path = realpath(DIR_APPLICATION . '../themes/') . '/' . $theme;
		$config_file = $theme_path . '/config.php';
		$setting_file = $theme_path . '/setting/setting.tpl';
		$languages = C('cache_language');
		$language_file = $theme_path . '/setting/language/' . $languages[C('config_admin_language')]['filename'] . '.php';
		
		if ($this->request->isPost() && $this->hasMP()) {
			$string = "<?php return ".var_export($this->request->post, true).";";
			file_put_contents($config_file, $string);
			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('design/theme'));
		}
		
		if (!file_exists($setting_file)) {
			$this->data['continue'] = UA('design/theme');
			$this->data['heading_title'] = $this->_['heading_title'];
			$this->data['message'] = $this->_['error_setting_file'];
			$this->display('common/message.tpl');
			return;
		}
		if (file_exists($config_file)) {
			$this->data['config_theme'] = include($config_file);
		}
		
		if (file_exists($language_file)) {
			$this->language->load($language_file, true);
		}
		elseif ($languages[C('config_admin_language')]['filename'] != 'english') {
			$language_file = $theme_path . '/setting/language/english.php';
			if (file_exists($language_file)) {
				$this->language->load($language_file, true);
			}
		}
		
		$this->data['action'] = UA('design/theme/setting', "theme=$theme");
		$this->template = $setting_file;
		$this->display();
	}
}
?>